#!/usr/bin/env bash
set -ex

command -v docker || exit 0  # docker not installed

for id in $(docker ps -aq)
do
    if docker inspect "$id" --format '{{.HostConfig.CgroupParent}} {{.Name}}' | grep kube
    then
        continue  # skip kube containers
    fi

    logger -s -t slurm-epilog "Removing docker container $id ..."

    docker stop "$id" || true
    if docker inspect "$id"
    then
        docker rm "$id"
    fi
done
